﻿#include "CardGame.h"

CardGame::CardGame()
{
	selfCards_node=Node::create();
}

cocos2d::Sprite* CardGame::getCardspriteWithCardValue(int cardValue)
{
	string file_name = "";

	if (cardValue == 52)
	{
		file_name = "xiaowang.png";
	}
	else if (cardValue == 53)
	{
		file_name = "dawang.png";
	}
	else
	{
		// 花色？
		int flower = cardValue / 13 + 1;
		// 牌面值？
		int value = cardValue % 13 + 1;
		if (flower == 1)
		{ // ♠️
			file_name = StringUtils::format("hei-%d.png", value);
		}
		else if (flower == 2)
		{ // ♦️
			file_name = StringUtils::format("fk-%d.png", value);
		}
		else if (flower == 3)
		{ // ♣️
			file_name = StringUtils::format("mh-%d.png", value);
		}
		else if (flower == 4)
		{ // ♥️
			file_name = StringUtils::format("hong-%d.png", value);
		}
	}

	Sprite *cardSprite = Sprite::createWithSpriteFrameName(file_name);
	cardSprite->setTag(cardValue);
	log("%d", cardValue);
	return cardSprite;
}

// 洗牌函数
void CardGame::shuffle(int a[], int len)
{
	int idx, tmp, i;
	for (i = 0; i < 54; i++) 
	{
		a[i] = i;
	}
	srand((unsigned)time(NULL));
	for (i = len - 1; i > 0; i--) 
	{
		idx = rand() % i; // 随机一个i范围内的数，然后和i交换位置
		tmp = a[i];
		a[i] = a[idx];
		a[idx] = tmp;
	}
	for (i = 0; i < len; i++) 
	{
		printf("%d ", a[i]);
	}

}

bool isKingBomb(vector<int> arr)
{
	if ((arr[0] == 52 && arr[1] == 53) || (arr[0] == 53 && arr[1] == 52))
	{
		return true;
	}
	else
	{
		return false;
	}
}

bool isRedTenBomb(vector<int> arr)
{
	if ((arr[0] == 22 && arr[1] == 48) || (arr[0] == 48 && arr[1] == 22))
	{
		return true;
	}
	else
	{
		return false;
	}
}

//判断是否有王
bool hasKing(vector<int> arr)
{
	for (int i = 0; i < arr.size(); i++)
	{
		if (arr[i] >= 52 )
		{
			return true;
		}
	}
	return false;
}

bool isDouble(vector<int> arr)
{
	//先排除大小王
	if (hasKing(arr))
	{
		return false;
	}
	else if (arr[0] % 13 == arr[1] % 13)
	{
		return true;
	}
	else
	{
		return false;
	}

}

bool isThreeOrFourBomb(vector<int> arr)
{
	//排除王的干扰
	if (hasKing(arr))
	{
		return false;
	}
	bool allSame = true;
	for (int i = 1; i < arr.size(); i++)
	{
		if (arr[0] % 13 != arr[i] % 13)
		{
			allSame = false;
			break;
		}
	}
	if (allSame)
	{
		return true;
	}
	else
	{
		return false;
	}
}

bool isStraight(vector<int> arr)
{
	if (hasKing(arr))
	{
		return false;
	}
	vector<int> vec(14, 0);
	for (int i = 0; i < arr.size(); i++)
	{
		vec[arr[i]%13] = vec[arr[i]%13] + 1;
	}
	//k
	if (vec[12] > 0 && vec[0] > 0)
	{
		vec[0]--;
		vec[13]++;
	}
	int count = 0;
	for (int i = 0; i < vec.size(); i++)
	{
		if (vec[i] == 1)
		{
			count++;
		}
		else if (count != 0)
		{
			break;
		}
	}
	if (count == arr.size())
	{
		return true;
	}
	return false;
}

PokerType CardGame::getPokerTypeWithCards(vector<int> arr) 
{
	//获取所选牌的数量
	int len = (int)arr.size();
	if (len == 1)
	{
		return PokerType::POKER_TYPE_SINGLE;
	}
	else if (len == 2)
	{
		if (isKingBomb(arr))
		{
			return PokerType::POKER_TYPE_KING_BOMB;
		}
		else if (isRedTenBomb(arr))
		{
			return PokerType::POKER_TYPE_REDTEN_BOMB;
		}
		else if (isDouble(arr))
		{
			return PokerType::POKER_TYPE_DOUBLE;
		}
		else if (len == 3||len == 4)
		{
			if (isThreeOrFourBomb(arr))
			{
				if (len == 3)
				{
					return PokerType::POKER_TYPE_THREE_BOMB;
				}
				else
				{
					return PokerType::POKER_TYPE_FOUR_BOMB;
				}
			}
			else if (isStraight(arr))
			{
				return PokerType::POKER_TYPE_STRAIGHT;
			}
		}
		else if (len > 4)
		{
			if (isStraight(arr))
			{
				return PokerType::POKER_TYPE_STRAIGHT;
			}
		}
	}
	return PokerType::POKER_TYPE_ERROR;
}